package com.decoration.basic.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.decoration.basic.BasicController;
import com.decoration.basic.entity.DecCarouselInf;
import com.decoration.basic.entity.JsonResult;
import com.decoration.basic.findCondition.DecCarouselFindCondition;
import com.decoration.basic.service.DecCarouselService;

@Controller
@RequestMapping("/admin")
public class CarouselController extends BasicController {

	@Resource
	private DecCarouselService decCarouselService;

	//轮播数据新增
	@RequestMapping(value = "/carousel/add", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult addCarousel(JsonResult jsonResult, @RequestBody DecCarouselInf decCarousel) {

		//传入参数校验
		if (null == decCarousel || "".equals(decCarousel)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要添加的轮播信息为空,请重试!");
			return jsonResult;
		}

		//总条数校验
		int count = decCarouselService.getDecCarouselInfSize();
		if (count > 9) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("添加失败,轮播最多只能有10条数据,请先删除!");
			return jsonResult;
		}

		//插入数据校验
		int res = decCarouselService.insert(decCarousel);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("轮播添加失败,请重试!");
			return jsonResult;
		}

		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("添加成功");
		return jsonResult;
	}

	@RequestMapping(value = "/carousel/update", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateCarousel(JsonResult jsonResult, @RequestBody DecCarouselInf decCarousel) {
		if (null == decCarousel || "".equals(decCarousel)) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的轮播信息为空,请重试!");
			return jsonResult;
		}
		if (null == decCarousel.getId() || "".equals(decCarousel.getId())) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要更新的轮播账号信息为空,请重试!");
			return jsonResult;
		}
		int res = decCarouselService.updateById(decCarousel);
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("轮播更新失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("更新成功");
		return jsonResult;
	}

	@RequestMapping(value = "/carousel/del", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult delCarousel(JsonResult jsonResult, @RequestBody DecCarouselInf decCarousel) {
		if (null == decCarousel.getId() || "".equals(decCarousel.getId())) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("需要删除的轮播账号信息为空,请重试!");
			return jsonResult;
		}
		int res = decCarouselService.deleteById(decCarousel.getId());
		if (res < 1) {
			jsonResult.setCode(JsonResult.FAILED_CODE);
			jsonResult.setMsg("轮播删除失败,请重试!");
			return jsonResult;
		}
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setMsg("删除成功");
		return jsonResult;
	}

	@RequestMapping(value = "/carousel/list", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getCarouselList(JsonResult jsonResult, @RequestBody DecCarouselFindCondition findCondition) {
		setPagerInfo(findCondition);
		List<DecCarouselInf> list = decCarouselService.findAll(findCondition);
		Integer count = decCarouselService.getDecCarouselInfSize();
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setCount(count);
		jsonResult.setData(list);
		return jsonResult;
	}

	@RequestMapping(value = "/carousel/{carouselId}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarousel(JsonResult jsonResult, @PathVariable String carouselId) {
		DecCarouselInf decCarousel = decCarouselService.findById(carouselId);
		jsonResult.setCode(JsonResult.SUCCESS_CODE);
		jsonResult.setData(decCarousel);
		return jsonResult;
	}
}
